/**
 * @param {string} s
 * @param {number} k
 * @return {number}
 */
var longestSubsequence = function(s, k) {
  let strK = k.toString(2) + "";
  let index = strK.length - 1, count = strK.length;
  // console.log(strK, index)
  for(let i = s.length - 1; i >= 0; i--) {
    if(s[i] == strK[index]) {
      index--;
    }
    // console.log(s[i], strK[index], index);
    if(index == -1) {
      for(let j = 0; j < i; j++) {
        if(s[j] == '0') count++;
      }
      break;
    }
  }
  return count;
};
let s = "1001010", k = 5;
console.log(longestSubsequence(s, k));
s = "00101001", k = 1;
console.log(longestSubsequence(s, k));
s = "0", k = 583196182;
console.log(longestSubsequence(s, k));
